package com.holystone.orca.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/*import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;*/

public class FileUtil {
	public static String createXml(String message,String path) throws FileNotFoundException{
		File file = new File(path.substring(0,path.lastIndexOf(File.separatorChar)));
		if(file.exists() && file.isDirectory()){
		}else{
			file.mkdir();
		}
		FileOutputStream fos = new FileOutputStream(new File(path));
		PrintStream ps = new PrintStream(fos);
		ps.print(message);
		ps.close();
		return new File(path).getAbsolutePath();
	} 
	public static String getFileText(String path) throws IOException{
		File file = new File(path);
		if(file.exists()){}
		else{
			if(file.isDirectory())
				return null;
		}
		BufferedReader br = null;
		try {
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			String text = "";
			String temp = "";
			do{
				text += temp + "\r";
				temp = br.readLine();
			}while(temp != null);
			return text;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}finally{
			br.close();
		}
			
	}
	public static List<String> getFileList(String path){
		if(path == null)
			throw new RuntimeException();
		List<String> fileList = new ArrayList<String>();
		File file = new File(path);
		if(!file.isDirectory()){
			return null;
		}
		File[] files = file.listFiles();
		for(File f:files){
			fileList.add(f.getAbsolutePath());
		}
		return fileList;
	}
	
	public static List<String> getLogFile(String path) throws IOException{
		List<String> logs = new ArrayList<String>();
		File file = new File(path);
		if(file.exists()){}
		else{
			if(file.isDirectory())
				return null;
		}
		BufferedReader br = null;
		try {
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			String temp = "";
			while(true){
				temp = br.readLine();
				if(temp == null){
					break;
				}
				logs.add(temp);
			}
			return logs;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}finally{
			br.close();
		}
	}
	
	/*public static Element readXMLFile(File file){
		SAXReader Reader = new SAXReader();
		Document doc = null;
		try {
			doc = Reader.read(file);
		} catch (DocumentException e) {
			return null;
		}
		Element root = doc.getRootElement();
		return root;
	}*/
	
	public static void deleteFile(String path){
		File file  = new File(path);
		if (file.exists()) {
			file.deleteOnExit();
		}
	}
	
	public static Element readXMLFile(File file){
		SAXReader Reader = new SAXReader();
		Document doc = null;
		try {
			doc = Reader.read(file);
		} catch (DocumentException e) {
			return null;
		}
		Element root = doc.getRootElement();
		return root;
	}
}
